package com.ThreadSyncEg;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

import java.io.InputStream;
import java.util.Random;
import java.util.Scanner;

public class ThreadWork1{

    //2个线程
    //第一个线程随机打印100以内的整数 直到第二个线程从键盘读取Q 结束


    public static void main(String[] args) throws InterruptedException {
        A a = new A();
        B b = new B(a);
        b.setDaemon(true);
        a.start();
        b.start();
        Thread.State state1 = b.getState();
        while (state1!=Thread.State.TERMINATED){
            state1 = b.getState();
            System.out.println("B线程状态====="+state1);
            Thread.sleep(1000);
        }
    }



}
class A extends Thread{
    boolean loop = true;

    public void setLoop(boolean loop) {
        this.loop = loop;
    }

    //随机输出1到100整数
    @Override
    public void run() {
     while (loop){
         System.out.println((int)(Math.random()*100+1));
         try {
             Thread.sleep(500);
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
     }

    }

}
class B extends Thread{
    private A a;//获取A类对象
    private Scanner sc = new Scanner(System.in);
    public B(A a) {
        this.a = a;
    }

    @Override
    public void run() {
        while (true){
            System.out.println("输入指令Q退出线程");
            char key = sc.next().toUpperCase().charAt(0);
            if(key == 'Q'){
                a.setLoop(false);
                System.out.println("线程退出");
                break;
            }
        }
    }
}
